package com.it.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.eduservice.entity.EduCourse;
import com.it.eduservice.entity.EduCourseDescription;
import com.it.eduservice.entity.EduTeacher;
import com.it.eduservice.entity.frontvo.CourseFrontVo;
import com.it.eduservice.entity.frontvo.CourseWebVo;
import com.it.eduservice.entity.vo.CourseInfoVo;
import com.it.eduservice.entity.vo.CoursePublishVo;
import com.it.eduservice.mapper.EduCourseMapper;
import com.it.eduservice.service.EduChapterService;
import com.it.eduservice.service.EduCourseDescriptionService;
import com.it.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.eduservice.service.EduVideoService;
import com.it.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2021-03-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //注入描述
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    //注入小结
    @Autowired
    private EduVideoService eduVideoService;
    //注入章节
    @Autowired
    private EduChapterService eduChapterService;

    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表中添加基本信息
        //CourseInfoVo转换成EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0){
            throw new GuliException(20001,"添加课程失败");
        }
        //成功后获取课程id
        String cid = eduCourse.getId();

        //2 向课程简介中加入简介
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置课程id和描述id相同
        courseDescription.setId(cid);
        eduCourseDescriptionService.save(courseDescription);

        return cid;
    }


    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查出课程表 根据课程id
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //封装到courseInfoVo中
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查出描述表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息 修改两张表 课程表 描述表
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(description);
    }

    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //根据课程id 删除小结 章节  课程描述 课程本身
    public void removeCourse(String courseId) {
        //根据课程id 删除小结
        eduVideoService.removeVideoByCourseId(courseId);
        //根据课程id 删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //根据课程id 删除描述
        eduCourseDescriptionService.removeById(courseId);
        //根据课程id 删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result==0){
            throw new GuliException(20001,"删除失败");
        }
    }

    //前台分页查询课程
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse>wrapper=new QueryWrapper<EduCourse>();
        //一级分类
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        //二级分类
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        //销量
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        //
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("gmt_create");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        long current = pageParam.getCurrent();
        long size = pageParam.getSize();
        long pages = pageParam.getPages();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object>map=new HashMap<String, Object>();
        map.put("items",records);
        map.put("total",total);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

    //根据课程id，编写sql语句查询课程信息

    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }




}
